﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Framework.Cache;
using CYTS.Aoyou.Framework.CAInterface;

namespace CYTS.Aoyou.Framework.CacheAccess
{
    public class PerformanceCacheAdapterForRedis : CacheAdapterBase, IPerformanceCacheAdapter, ICacheAdapter
    {
        public PerformanceCacheAdapterForRedis(CacheProvider provider)
            : base(provider)
        { }

        /// <summary>
        /// 设置radis的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expireTime">设置完成后，通过此时间间隔，自动删除此key</param>
        public void Set<T>(string key, T value, TimeSpan expireTime)
        {
            List<CacheConnection> connectionList = null;
            if (!this.DicCacheConnection.TryGetValue("PerformanceCache", out connectionList) || connectionList.Count == 0)
            {
                throw new Exception("未找到对应的缓存连接串");
            }

            CacheConnection connection = connectionList[0];
            RedisHelper helper = new RedisHelper(connection.Host, connection.Port, Convert.ToInt32(connection.RegionName), connection.Password, this.Region);
            helper.Add(key, value, expireTime);
        }

        /// <summary>
        /// 获取radis的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            List<CacheConnection> connectionList = null;
            if (!this.DicCacheConnection.TryGetValue("PerformanceCache", out connectionList) || connectionList.Count == 0)
            {
                throw new Exception("未找到对应的缓存连接串");
            }

            CacheConnection connection = connectionList[0];
            RedisHelper helper = new RedisHelper(connection.Host, connection.Port, Convert.ToInt32(connection.RegionName), connection.Password, this.Region);
            return helper.Get<T>(key);
        }

        /// <summary>
        /// 删除radis
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            List<CacheConnection> connectionList = null;
            if (!this.DicCacheConnection.TryGetValue("PerformanceCache", out connectionList) || connectionList.Count == 0)
            {
                throw new Exception("未找到对应的缓存连接串");
            }

            CacheConnection connection = connectionList[0];
            RedisHelper helper = new RedisHelper(connection.Host, connection.Port, Convert.ToInt32(connection.RegionName), connection.Password, this.Region);
            helper.Remove(key);
        }

        /// <summary>
        /// 设置radis值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Set<T>(string key, T value)
        {
            List<CacheConnection> connectionList = null;
            if (!this.DicCacheConnection.TryGetValue("PerformanceCache", out connectionList) || connectionList.Count == 0)
            {
                throw new Exception("未找到对应的缓存连接串");
            }

            CacheConnection connection = connectionList[0];
            RedisHelper helper = new RedisHelper(connection.Host, connection.Port, Convert.ToInt32(connection.RegionName), connection.Password, this.Region);
            helper.Add(key, value);
        }
    }
}
